/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;

import java.awt.Color;
import java.awt.EventQueue;
import java.beans.Beans;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.List;
import javax.persistence.RollbackException;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.StyleConstants;

/**
 *
 * @author Khalid
 */
public class HstqStockForm extends JPanel {

    public HstqStockForm() {
        initComponents();
        if (!Beans.isDesignTime()) {
            entityManager.getTransaction().begin();
        }
        System.out.print("*****************************************************");
        refreshButton.doClick();
//        java.util.Collection data = query.getResultList();
//        for (Object entity : data) {
//            entityManager.refresh(entity);
//        }
//        list.clear();
//        list.addAll(data);
      
//        refreshButtonActionPerformed(null);
        // System.out.print(masterTable.getColumnModel().getColumn(2).getHeaderValue());
//        masterTable.removeColumn(masterTable.getColumnModel().getColumn(0));
//        masterTable.removeColumn(masterTable.getColumnModel().getColumn(2));
        jLabel1.setVisible(false);
    }

    boolean validForm() {
        boolean isInvalid = true;
        boolean isEmpty = true;

        if (qteField.getText().equalsIgnoreCase("") && qteField.isEnabled()) {
            qteField.setBorder(BorderFactory.createLineBorder(Color.RED));
            qteLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isEmpty = false;
        } else {
            try {
                if (qteField.isEnabled()) {
                    Double.parseDouble(qteField.getText());
                }
                qteField.setBorder(BorderFactory.createEtchedBorder());
                qteLabel.setForeground(Color.BLACK);
                masterTable.setEnabled(true);
            } catch (Exception ex) {
                qteField.setBorder(BorderFactory.createLineBorder(Color.RED));
                qteLabel.setForeground(Color.RED);
                masterTable.setEnabled(false);
                isInvalid = false;
            }
        }

        if (jcmbPrdt.getSelectedIndex() == -1 && qteField.isEnabled()) {
            jcmbPrdt.setBorder(BorderFactory.createLineBorder(Color.RED));
            produitIdLabel.setForeground(Color.RED);
            masterTable.setEnabled(false);
            isEmpty = false;
        } else {
            jcmbPrdt.setBorder(BorderFactory.createEtchedBorder());
            produitIdLabel.setForeground(Color.BLACK);
            masterTable.setEnabled(true);

        }

        boolean temp;
        if (!isEmpty) {
            temp = isEmpty;
            jLabel1.setText("Veuillez remplir les champs obligatoires");
        } else {
            temp = isInvalid;
            jLabel1.setText("Les information saisi sont incorrecte");
        }
        jLabel1.setVisible(!temp);
        return temp;
    }
    int nbrOpertation = 0;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("gestionPU").createEntityManager();
        query = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT h FROM HstqStock h");
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        produitQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Produit p");
        produitList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : produitQuery.getResultList();
        clientQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT c FROM Client c");
        clientList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : clientQuery.getResultList();
        produitQuery1 = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM Produit p");
        produitList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : produitQuery1.getResultList();
        produitListCellRenderer1 = new presentation.ProduitListCellRenderer();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jcmbPrdt = new javax.swing.JComboBox();
        jSpDate = new javax.swing.JSpinner();
        qteLabel = new javax.swing.JLabel();
        produitIdLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();
        qteField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        produitListCellRenderer1.setText("produitListCellRenderer1");

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${date}"));
        columnBinding.setColumnName("Date");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${produitId.libelle}"));
        columnBinding.setColumnName("Produit");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${produitId}"));
        columnBinding.setColumnName("Produit Id");
        columnBinding.setColumnClass(entity.Produit.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${qte}"));
        columnBinding.setColumnName("Qte");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterScrollPane.setViewportView(masterTable);
        if (masterTable.getColumnModel().getColumnCount() > 0) {
            masterTable.getColumnModel().getColumn(3).setMinWidth(0);
            masterTable.getColumnModel().getColumn(3).setPreferredWidth(0);
            masterTable.getColumnModel().getColumn(3).setMaxWidth(0);
        }

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Détailles: "));

        jcmbPrdt.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jcmbPrdt.setRenderer(produitListCellRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, produitList1, jcmbPrdt);
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.produitId}"), jcmbPrdt, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), jcmbPrdt, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jcmbPrdt.addFocusListener(formListener);
        jcmbPrdt.addPropertyChangeListener(formListener);

        jSpDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(1393708676531L), null, null, java.util.Calendar.DAY_OF_MONTH));
        jSpDate.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jSpDate.setEditor(new javax.swing.JSpinner.DateEditor(jSpDate, "dd/MM/yyyy"));
        jSpDate.setEnabled(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.date}"), jSpDate, org.jdesktop.beansbinding.BeanProperty.create("value"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), jSpDate, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jSpDate.addPropertyChangeListener(formListener);

        qteLabel.setText("Quantitée:");

        produitIdLabel.setText("Produit:");

        dateLabel.setText("Date:");

        qteField.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.qte}"), qteField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue("");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), qteField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        qteField.addFocusListener(formListener);

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Veuillez remplir les champs obligatoires");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        deleteButton.setText("Delete");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        newButton.setText("New");
        newButton.addActionListener(formListener);

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(formListener);

        saveButton.setText("Save");
        saveButton.addActionListener(formListener);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 408, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dateLabel)
                            .addComponent(produitIdLabel)
                            .addComponent(qteLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(qteField, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 125, Short.MAX_VALUE)
                                .addComponent(newButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deleteButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(refreshButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(saveButton))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcmbPrdt, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jSpDate, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {deleteButton, newButton, refreshButton, saveButton});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(dateLabel)
                            .addComponent(jSpDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(produitIdLabel)
                            .addComponent(jcmbPrdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(qteLabel)
                            .addComponent(qteField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(22, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(saveButton)
                            .addComponent(refreshButton)
                            .addComponent(deleteButton)
                            .addComponent(newButton)))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(masterScrollPane))
                .addGap(5, 5, 5))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12))
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.awt.event.FocusListener, java.beans.PropertyChangeListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == deleteButton) {
                HstqStockForm.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                HstqStockForm.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                HstqStockForm.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == saveButton) {
                HstqStockForm.this.saveButtonActionPerformed(evt);
            }
        }

        public void focusGained(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == jcmbPrdt) {
                HstqStockForm.this.jcmbPrdtFocusGained(evt);
            }
            else if (evt.getSource() == qteField) {
                HstqStockForm.this.qteFieldFocusGained(evt);
            }
        }

        public void focusLost(java.awt.event.FocusEvent evt) {
            if (evt.getSource() == jcmbPrdt) {
                HstqStockForm.this.jcmbPrdtFocusLost(evt);
            }
            else if (evt.getSource() == qteField) {
                HstqStockForm.this.qteFieldFocusLost(evt);
            }
        }

        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            if (evt.getSource() == jcmbPrdt) {
                HstqStockForm.this.jcmbPrdtPropertyChange(evt);
            }
            else if (evt.getSource() == jSpDate) {
                HstqStockForm.this.jSpDatePropertyChange(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    @SuppressWarnings("unchecked")
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        entityManager.getTransaction().rollback();
        entityManager.getTransaction().begin();
        java.util.Collection data = query.getResultList();
        for (Object entity : data) {
            entityManager.refresh(entity);
        }
        list.clear();
        list.addAll(data);
        
        this.validForm();
        nbrOpertation = 0;
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int[] selected = masterTable.getSelectedRows();
        List<entity.HstqStock> toRemove = new ArrayList<entity.HstqStock>(selected.length);
        for (int idx = 0; idx < selected.length; idx++) {
            entity.HstqStock h = list.get(masterTable.convertRowIndexToModel(selected[idx]));
            toRemove.add(h);
            entityManager.remove(h);
        }
        list.removeAll(toRemove);
        this.validForm();
        nbrOpertation += selected.length;
    }//GEN-LAST:event_deleteButtonActionPerformed
    boolean valid;
    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        if (!valid && !this.validForm()) {
            return;
        }
        entity.HstqStock h = new entity.HstqStock();
        h.setDate(new Date());
        entityManager.persist(h);
        list.add(h);
        int row = list.size() - 1;
        masterTable.setRowSelectionInterval(row, row);
        masterTable.scrollRectToVisible(masterTable.getCellRect(row, 0, true));
        valid = this.validForm();
        nbrOpertation += 1;
    }//GEN-LAST:event_newButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if (!validForm()) {
            return;
        }
        try {
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            if (nbrOpertation > 0) {
                JOptionPane.showMessageDialog(this, "L'opération est terminé avec succée", "Information", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (RollbackException rex) {
            rex.printStackTrace();
            entityManager.getTransaction().begin();
            List<entity.HstqStock> merged = new ArrayList<entity.HstqStock>(list.size());
            for (entity.HstqStock h : list) {
                merged.add(entityManager.merge(h));
            }
            list.clear();
            list.addAll(merged);
        }
        nbrOpertation = 0;
    }//GEN-LAST:event_saveButtonActionPerformed

    private void jSpDatePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSpDatePropertyChange
        // TODO add your handling code here:
//        Date temp = new Date();
//        System.out.print(temp.getTime());
//        jSpDate.setValue(Long.parseLong("13937767768521393776777095"));
        if (evt.getPropertyName().equals("enabled") && evt.getNewValue().toString().equals("false")) {
            Calendar calendar = new GregorianCalendar();
            jSpDate.setValue(calendar.getTime());
        }
    }//GEN-LAST:event_jSpDatePropertyChange

    private void jcmbPrdtPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jcmbPrdtPropertyChange
        // TODO add your handling code here:
        if (evt.getPropertyName().equals("enabled") && evt.getNewValue().toString().equals("false")) {
            jcmbPrdt.setSelectedIndex(-1);
        }
    }//GEN-LAST:event_jcmbPrdtPropertyChange

    int idxProduitcmb = -1;
    private void jcmbPrdtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jcmbPrdtFocusGained
        // TODO add your handling code here:

        idxProduitcmb = jcmbPrdt.getSelectedIndex();

    }//GEN-LAST:event_jcmbPrdtFocusGained

    private void jcmbPrdtFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jcmbPrdtFocusLost
        // TODO add your handling code here:
        if (jcmbPrdt.getSelectedIndex() != idxProduitcmb) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_jcmbPrdtFocusLost

    String qtefd;
    private void qteFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qteFieldFocusGained
        // TODO add your handling code here:
        this.qtefd = qteField.getText();
    }//GEN-LAST:event_qteFieldFocusGained

    private void qteFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_qteFieldFocusLost
        // TODO add your handling code here:
        if (!qtefd.equals(qteField.getText())) {
            nbrOpertation += 1;
        }
    }//GEN-LAST:event_qteFieldFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.List<entity.Client> clientList;
    private javax.persistence.Query clientQuery;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSpinner jSpDate;
    private javax.swing.JComboBox jcmbPrdt;
    private java.util.List<entity.HstqStock> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JButton newButton;
    private javax.swing.JLabel produitIdLabel;
    private java.util.List<entity.Produit> produitList;
    private java.util.List<entity.Produit> produitList1;
    private presentation.ProduitListCellRenderer produitListCellRenderer1;
    private javax.persistence.Query produitQuery;
    private javax.persistence.Query produitQuery1;
    private javax.swing.JTextField qteField;
    private javax.swing.JLabel qteLabel;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HstqStockForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HstqStockForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HstqStockForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HstqStockForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new HstqStockForm());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

}
